package com.zhiyou.aifeng.smart_machine

import android.annotation.SuppressLint
import android.os.Bundle
import com.aifeng.utils.getExtra
import com.jeremyliao.liveeventbus.LiveEventBus
import com.zhiyou.aifeng.common.base.*
import com.zhiyou.aifeng.common.helper.get
import com.zhiyou.aifeng.common.helper.getTimeString
import com.zhiyou.aifeng.common.helper.optNotNull
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.base.loadRoundedImage
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.common_ui.utils.showHintDialog
import com.zhiyou.aifeng.viewmodel.MachineViewModel
import kotlinx.android.synthetic.main.activity_machine_detail.*
import org.jetbrains.anko.textColor
import org.jetbrains.anko.toast
import org.json.JSONObject

class MachineDetailActivity : BaseActivity() {

    private lateinit var mMachineId: String
    private var mMachineStatus = false
    private var mMachineType: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_machine_detail)
        setTransparentAndToolbar(isDarkFont = false)
        initTitle()

        getData()
    }

    override fun getViewModel() = get<MachineViewModel>()

    override fun initTitle(title: String, name: String, divider: Boolean) {
        mMachineId = intent.getExtra("id")

        machine_right.oneClick { mMachineType?.let { deleteItem(mMachineId) } }
        machine_switch.oneClick { mMachineType?.let { changeStatus(mMachineId, !mMachineStatus) } }
    }

    private fun changeStatus(brandId: String, status: Boolean) {
        getViewModel().request(
            requestUrl = BaseUrl.status,
            params = *arrayOf(
                "id" to brandId,
                "toolStatus" to status
            ),
            onResult = {
                toast("修改状态成功！")

                val position = intent.getIntExtra("index", -1)
                LiveEventBus.get("machineList").post("update,$position")

                mMachineStatus = !mMachineStatus
                machine_status.textColor =
                    getColorEx(
                        if (mMachineStatus) R.color.colorAccent
                        else R.color.gray
                    )
                machine_status.text = if (mMachineStatus) "正常" else "不可用"
                machine_switch.setImageResource(
                    if (mMachineStatus) R.mipmap.icon_open
                    else R.mipmap.icon_close
                )
            }
        )
    }

    private fun deleteItem(brandId: String) {
        showHintDialog(
            title = "删除农${if (mMachineType == "1") "具" else "机"}",
            hint = "确定要删除农${if (mMachineType == "1") "具" else "机"}吗？"
        ) {
            getViewModel().request(
                requestUrl = BaseUrl.delBrand,
                params = *arrayOf("id" to brandId),
                onResult = {
                    toast("删除成功！")
                    val position = intent.getIntExtra("index", -1)
                    LiveEventBus.get("machineList").post("delete,$position")
                    finishView()
                }
            )
        }
    }

    @SuppressLint("SetTextI18n")
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().request(
            requestUrl = BaseUrl.brandDetail,
            params = *arrayOf("id" to mMachineId),
            onResult = {
                val obj = JSONObject(it)

                mMachineType = obj.optNotNull("type")

                machine_img.loadRoundedImage(
                    url = obj.optNotNull("img"),
                    radius = 7f,
                    resourceId = R.mipmap.default_logo2
                )

                val brandName = obj.optNotNull("brandName")
                val typeName = obj.optNotNull("vehicleTypeName")
                val modelName = obj.optNotNull("modelName")
                machine_name.text = "${brandName + typeName}/$modelName"

                val checkStatus = obj.optNotNull("checkStatus")
                mMachineStatus = obj.optBoolean("toolStatus", false)

                machine_status.textColor =
                    getColorEx(
                        if (mMachineStatus) R.color.colorAccent
                        else R.color.gray
                    )
                machine_status.text = if (mMachineStatus) "正常" else "不可用"

                when (checkStatus) {
                    "0" -> {
                        machine_check.text = "审核中"
                        machine_status.text = "正常"
                        machine_status.textColor = getColorEx(R.color.colorAccent)
                        machine_check.visible()
                        machine_switch.invisible()
                    }
                    "1" -> {
                        machine_check.text = "已通过"
                        machine_check.gone()
                        machine_switch.visible()
                    }
                    "2" -> {
                        machine_check.text = "未通过"
                        machine_status.text = "正常"
                        machine_status.textColor = getColorEx(R.color.colorAccent)
                        machine_check.visible()
                        machine_switch.invisible()
                    }
                }

                machine_switch.setImageResource(
                    if (mMachineStatus) R.mipmap.icon_open
                    else R.mipmap.icon_close
                )

                machine_type.text = typeName
                machine_brand.text = brandName
                machine_num.text = modelName
                machine_suit.text = obj.optNotNull("work_name")
                machine_time.text = obj.optNotNull("productionTime").getTimeString("yyyy-MM-dd")

                if (mMachineType == "0") {
                    layout_extra.visible()
                    machine_code.text = obj.optNotNull("engineNo")
                    machine_power.text = "${obj.optNotNull("horsepower")}匹"
                    machine_car.text = obj.optNotNull("plateNo")
                } else {
                    view4.text = "农具类型："
                    view5.text = "农具品牌："
                    view6.text = "农具型号："
                }
            }
        )
    }

}